﻿using System;
using System.Collections.Generic;

namespace Tatibah
{
    /// <summary>
    /// This class represents a Paragraph within a Document
    /// </summary>
    public class Paragraph : BaseParagraphFormatting, iDocumentAppend, iCellAppend
    {
  
        /// <summary>
        /// When set casues a Page Break before the Paragraph
        /// </summary>
        public bool PageBreakBefore { get; set; } 

        private Style style = null;
        
        private List<Run> internalRuns = null;

        private Run currentRun = null;

        #region C O N S T R U C T O R S
        /// <summary>
        /// Empty Paragraph
        /// </summary>
        public Paragraph()
        {
            internalRuns = new List<Run>();
        }

        /// <summary>
        /// A Paragraph constructed from a string the supplied MultipleStrings. 
        /// </summary>
        /// <param name="Text"></param>
        public Paragraph(String Text)
        {
            internalRuns = new List<Run>();
            currentRun = new Run(Text);
            internalRuns.Add(currentRun);
        }

        /// <summary>
        /// A paragraph based on the Run
        /// </summary>
        /// <param name="Run"></param>
        public Paragraph(Run Run)
        {
            internalRuns = new List<Run>();
            currentRun = Run;
            internalRuns.Add(currentRun);
        }

        /// <summary>
        /// Create a Paragraph from an array of differnt items. Items include Runs and Images
        /// </summary>
        /// <param name="MultipleItems"></param>
        public Paragraph(params iParagraphAppend[] MultipleItems)
        {
            foreach (iParagraphAppend item in MultipleItems)
            {
                if (item.GetType() == typeof(Run))
                    Append((Run)item);
                else if (item.GetType() == typeof(Image))
                    Append((Image)item);
            }
        }


        #endregion C O N S T R U C T O R S

        #region P R O P E R T I E S

        /// <summary>
        /// A Paragraph is a List of Internal Runs this Property returns that List
        /// </summary>
        public List<Run> InternalRuns
        {
            get { return internalRuns; }
        }

        /// <summary>
        /// When set preverses trailing spaces, default is true
        /// </summary>
        public bool PreserveSpacing
        {
            get { return currentRun.PreserveSpacing; }
            set { currentRun.PreserveSpacing = value; }
        }
        /// <summary>
        /// This format only applies to the last bit of text Appended.
        /// </summary>
        public Run FormatLastAddedRun
        {
            get { return currentRun; }
        }

        /// <summary>
        /// Get or set the Style of the Paragraph
        /// </summary>
        public Style Style
        {
            get { return style; }
            set { style = value; }
            
        }

        #endregion P R O P E R T I E S

        /// <summary>
        /// Append a string to a paragraph, the string is converted to a Run
        /// </summary>
        /// <param name="Text"></param>
        public void Append(string Text)
        {
            if (internalRuns == null) internalRuns = new List<Run>();
            currentRun = new Run(Text);
            internalRuns.Add(currentRun);
        }

        /// <summary>
        /// Append a Run to a Paragraph
        /// </summary>
        /// <param name="Run"></param>
        public void Append(Run Run)
        {
            if (internalRuns == null) internalRuns = new List<Run>();
            currentRun = Run;
            internalRuns.Add(currentRun);
        }

        /// <summary>
        /// Add an Image to an already existing Paragraph
        /// </summary>
        /// <param name="ThisImage"></param>
        public void Append(Image ThisImage)
        {
            if (internalRuns == null) internalRuns = new List<Run>();
            currentRun = new Run(ThisImage);
            internalRuns.Add(currentRun);
        }

        /// <summary>
        /// Append multiple Runs, and/or Tables 
        /// </summary>
        /// <param name="MultipleItems">Comma seperated list to add</param>
        public void Append(params iParagraphAppend[] MultipleItems)
        {

            foreach (iParagraphAppend item in MultipleItems)
            {
                if (item.GetType() == typeof(Run))
                    Append((Run)item);
                else if (item.GetType() == typeof(Image))
                    Append((Image)item);

            }
        }

        /// <summary>
        /// Append the current Page Number, most useful in Headers and Footer. 
        /// </summary>
        public void AppendPageNumber()
        {
            if (internalRuns == null) internalRuns = new List<Run>();
            currentRun = new Run(" PAGE ", enumRunType.FieldCode);
            internalRuns.Add(currentRun);
        }

        /// <summary>
        /// Append the Total Number of Pages, most useful in Headers and Footer. 
        /// </summary>
        public void AppendNumberOfPages()
        {
            if (internalRuns == null) internalRuns = new List<Run>();
            currentRun = new Run(" NUMPAGES ", enumRunType.FieldCode);
            internalRuns.Add(currentRun);
        }

    }
}
